package pacBiopackage;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import tools.kmer.KmerSet_binary;


public class PacBioKmerSeeding 
{
	List<Integer> notCorrected = null;
	private PacBioReadContainer read = null;
	private KmerSet_binary ks=null;
	private List<Integer>seeds=null;
	public PacBioKmerSeeding(List<Integer>corrected,PacBioReadContainer read, KmerSet_binary ks) 
	{
		this.read=read;
		this.ks=ks;
	}
	
	public void seeding()
	{
		seeds= new ArrayList<Integer>();
		if(read.getPositionsList().size()==0)
		{
			//no correction-> naive Kmersearch!
			PacBioSeedFinding pbsf= new PacBioSeedFinding(read,ks);
			pbsf.findSeed();
		}
		else
		{	
			int i=0;
			while(i<read.getPositionsList().size())
			{
					//kmer?
				 int new_position =buildSeed(i);
				 //find correct i;
				 while(read.getPositionsList().get(i)<new_position && i < read.getPositionsList().size())
				 {
					 ++i;
				 }
				 ++i;
			}
		}
		//now we have to convert into notCorrectedPositions;
		int i=0;
		notCorrected = new ArrayList<Integer>();
		int startingPoint=0;
		while(i+1<seeds.size())
		{
			if(i==0)
			{
				for(int j=seeds.get(i); j >=0; --j)
				{
					notCorrected.add(j);
				}
				startingPoint=seeds.get(i+1);
			}
			else
			{
				for(int j=startingPoint; j<seeds.get(i);++j)
				{
					notCorrected.add(j);
				}
				startingPoint=seeds.get(i+1);
			}
		}
		if(seeds.get(seeds.size()-1)!=read.getOrginalSequence().length())
		{
			for(int j=seeds.get(seeds.size()-1); j< read.getOrginalSequence().length();++j)
			{
				notCorrected.add(j);
			}
		}
		Collections.sort(notCorrected);
		read.setNotCorrectedPositions(notCorrected);
	}
	

	
	private int buildSeed(int i)
	{
		
		int position=read.getPositionsList().get(i);//Centerpoint
		if(seeds.contains(position))
		{
			return position;
		}
		//now build up a kmer
		int halfPoint = ks.getKmerSize()/2;
		
		StringBuilder sb = new StringBuilder();
		int endPosition=position;
		int startPosition=position;
		if(read.getOrginalSequence().length()<ks.getKmerSize())
		{
			System.err.println("Template is to short!!");
			System.exit(1);
		}
		if(position<halfPoint)
		{
			sb.append(read.getOrginalSequence().substring(0,ks.getKmerSize()+1));
			//sb.append(read.getOrginalSequence().substring(position,ks.getKmerSize()-position));
			startPosition=0;
			endPosition=ks.getKmerSize();
		}
		else if(position+halfPoint+2>=read.getOrginalSequence().length())
		{
			//int endDistance = read.getOrginalSequence().length()-position;
			sb.append(read.getOrginalSequence().substring(read.getOrginalSequence().length()-(ks.getKmerSize()+1),read.getOrginalSequence().length()));		
			startPosition=read.getOrginalSequence().length()-(ks.getKmerSize()+1);
			endPosition=read.getOrginalSequence().length();
		}
		else
		{
			sb.append(read.getOrginalSequence().substring(position-halfPoint,position));
			startPosition = position-halfPoint;
			if(ks.getKmerSize()%2==0)
			{
				sb.append(read.getOrginalSequence().substring(position,position+halfPoint+1));
				endPosition=position+halfPoint+1;
			}
			else
			{
				sb.append(read.getOrginalSequence().substring(position,position+halfPoint+2));
				endPosition=position+halfPoint+2;
			}
		}
		if(ks.checkKmerDistribution(sb.toString())!=0)
		{
			if(seeds.size()!=0)
			{
				if(seeds.get(seeds.size()-1)>= startPosition)
				{
					seeds.set(seeds.size()-1,endPosition);
				}
				else
				{
					seeds.add(startPosition-1);
					seeds.add(endPosition);	
				}
			}
			else
			{
				seeds.add(startPosition-1);
				seeds.add(endPosition);
			}
			
		}
		return endPosition;
	}
}
